//
//  ST_LoadView.m
//  ST_BaseMall
//
//  Created by 昱胜 on 2022/9/2.
//

#import "ST_LoadView.h"

@interface ST_LoadView ()

/* backView */
@property (nonatomic, strong) UIView *backView;

/* emptyImgView */
@property (nonatomic, strong) UIImageView *emptyImgView;

/* images */
@property (nonatomic, strong) NSMutableArray *images;

/* loaderData */
@property (nonatomic, strong) UIImage *loaderData;

@end

@implementation ST_LoadView

+ (ST_LoadView *)shareEmptyView {
    static ST_LoadView *emptyView = nil;
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        emptyView = [[ST_LoadView alloc]init];
    });
    
    return emptyView;
}

- (void)show {
    [kSTWindow addSubview:self.backView];
    [kSTWindow addSubview:self.emptyImgView];
    
    self.backView.frame = kSTWindow.bounds;
    self.emptyImgView.center = kSTWindow.center;
}

- (void)dismiss {
    [self.backView removeFromSuperview];
    [self.emptyImgView removeFromSuperview];
}

#pragma mark  lazy
-(UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _backView.backgroundColor = [UIColor clearColor];
    }
    return _backView;
}

- (UIImageView *)emptyImgView {
    if (!_emptyImgView) {
        _emptyImgView = [[UIImageView alloc]initWithImage:self.loaderData];
    }
    return _emptyImgView;
}

- (UIImage *)loaderData{
    if (!_loaderData) {
        NSArray *datas = self.images;
        _loaderData = [UIImage animatedImageWithImages:datas duration:0.35];
    }
    return _loaderData;
}

- (NSMutableArray *)images{
    if (!_images) {
        _images = @[].mutableCopy;
        for (int i = 1; i <= 8; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"load%@", @(i)]];
            if (!image) {
                break;
            }
            [_images addObject:image];
        }
    }
    return _images;
}

@end
